package com.yonyou.gmmc.service.pointactivity.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.service.pointactivity.dao.NotReturnFactoryMapper;
import com.yonyou.gmmc.service.pointactivity.dao.QuestionnaireMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TmPotentialUserMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TrUserOwnerMapper;
import com.yonyou.gmmc.service.pointactivity.model.QuestionBo;
import com.yonyou.gmmc.service.pointactivity.model.TmAnswerInfo;
import com.yonyou.gmmc.service.pointactivity.model.TtQneResult;


@Transactional
@Service("QuestionnaireService")
public class QuestionnaireService {
	
	@Autowired
	private QuestionnaireMapper questionnaireMapper;
	
	@Autowired
	private TmPotentialUserMapper tmPotentialUserMapper;
	
	@Autowired
	private TrUserOwnerMapper trUserOwnerMapper;
	
	@Autowired
	private NotReturnFactoryMapper notReturnFactoryMapper;
		
	
	/**
	 * 下一题跳转
	 * @param map
	 * @return
	 */
	public QuestionBo queryNextQuestionByQtnNo(Map<String, Object> map){
		long qtnNo = Long.valueOf(map.get("qtnNo").toString());
		if(qtnNo == 2 || qtnNo == 12){
			QuestionBo bo = new QuestionBo();
			if(qtnNo == 2){
				bo.setQtnNo(Long.valueOf(2));
			}
			else{
				bo.setQtnNo(Long.valueOf(12));
			}
			return bo;
		}
		if(qtnNo == 1){
			String answer = map.get("answer").toString();
			String answerYes = questionnaireMapper.getAnswerYes();
			if(answerYes != null && !"".equals(answerYes)){
				if(answerYes.equals(answer)){
					map.put("qtnNo", qtnNo + 2);
				}else{
					map.put("qtnNo", qtnNo + 1);
				}
			}
		}else{
			map.put("qtnNo", qtnNo + 1);
		}
		return queryQuestionByQtnNo(map);
	}
	
	/**
	 * 按题号跳转
	 */
	public QuestionBo queryQuestionByQtnNo(Map<String, Object> map){
		List<QuestionBo> list = questionnaireMapper.queryQuestionByQtnNo(map);
		List<TmAnswerInfo> lista = new ArrayList<>();
		for (QuestionBo questionBo : list) {
			TmAnswerInfo tmAnswerInfo = new TmAnswerInfo();
			tmAnswerInfo.setAnswer(questionBo.getAnswerTitle());
			if(questionBo.getAnswerId() != null){
				tmAnswerInfo.setAnswerId(questionBo.getAnswerId());
			}
			tmAnswerInfo.setQtnId(questionBo.getQtnId());
			if(questionBo.getAnswerNo() != null){
				tmAnswerInfo.setAnswerNo(questionBo.getAnswerNo());
			}
			lista.add(tmAnswerInfo);
		}
		QuestionBo bo = list.get(0);
		bo.setList(lista);
		return bo;
	}
	
	/**
	 * 上一题跳转
	 * @param map
	 * @return
	 */
	public QuestionBo queryLastQuestionByQtnNo(Map<String, Object> map){
		long qtnNo = Long.valueOf(map.get("qtnNo").toString());
		if(qtnNo == 3){
			map.put("qtnNo", qtnNo-2);
		}else{
			map.put("qtnNo", qtnNo-1);
		}
		if(qtnNo == 3 || qtnNo == 2){
			questionnaireMapper.deleteAfterFirstQuestionAnswer(map);
		}
		return queryQuestionByQtnNo(map);
	}

	public QuestionBo queryQuestion(Map<String, Object> map) {
		
		//判断是否过题
		List<QuestionBo> listRs = questionnaireMapper.queryIfAnswer(map);
		long qtnNo = 0;
		if(listRs.size() > 0){ //说明答过题
			qtnNo = listRs.get(0).getQtnNo();
			map.put("answer", listRs.get(0).getAnswerTitle());
		}
		map.put("qtnNo", qtnNo);
		return queryNextQuestionByQtnNo(map); 
	}


	/**
	 * 先删除后保存
	 * @param map
	 * @param ttQneResult
	 */
	public void saveAnswer(Map<String, Object> map,TtQneResult ttQneResult) {
		questionnaireMapper.deleteByQtnAndUserId(map);
		questionnaireMapper.insert(ttQneResult);
		long qtnNo = Long.valueOf(map.get("qtnNo").toString());
		if(qtnNo == 2 || qtnNo == 12){
			Timestamp t = new Timestamp(System.currentTimeMillis());
			map.put("time",t);
			questionnaireMapper.updateReplaySubmitTime(map);
		}
	}
	
	
	/**
	 * 获取userId
	 * @param openId
	 * @return
	 */
	public String getUserId(String openId) throws Exception {
		Map<String, Object> potentialMap = queryPotentialUser(openId);

		Map<String, Object> memberMap = null != potentialMap ? queryMemberInfo(potentialMap.get("POTENTIAL_USER_ID").toString()) : null;

		return memberMap.get("USER_ID").toString();
	}
	
	public Map<String, Object> queryPotentialUser(String openId) {
		return tmPotentialUserMapper.queryPotentialUserByOpenId(openId);
	}
	
	public Map<String, Object> queryMemberInfo(String potentialUserId) {
		return trUserOwnerMapper.queryMemberInfoByPotentialUserId(potentialUserId);
	}

	/**
	 * 查询用户问卷号
	 * @param map
	 * @return
	 */
	public String queryId(Map<String, Object> map) {
		return questionnaireMapper.queryId(map);
	}

	/**
	 * 判断是否生成客户问卷
	 * @param map
	 * @return
	 */
	public String questionnaireIfExist(Map<String, Object> map) {
		String qId = queryId(map);
		if(qId == null){
			Timestamp t = new Timestamp(System.currentTimeMillis());
			map.put("time",t);
			questionnaireMapper.insertQuestionnaire(map);
			qId = queryId(map);
		}
		return qId;
	}
	
	/**
	 * 
	 * @param openId
	 * @param qneTitle
	 * @return 
	 */
	public String getQtnNoByOpenId(String openId,String qneTitle){
		Map<String, Object> map = new HashMap<>();
		map.put("openId", openId);
		map.put("qneTitle", qneTitle);
		return questionnaireMapper.getQtnNoByOpenId(map);
	}
	
	/**
	 * 
	 */
	public boolean isAnswer(Map<String, Object> map){
		return Boolean.valueOf(notReturnFactoryMapper.isAnswer(map));
	}
}
